/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aplikasirawatinap;
import Controller.Generate_md5;
import Controller.onlyLetter;
import Service.Koneksi;
import java.sql.*;
import javax.swing.*;
import java.sql.ResultSet;
import java.text.NumberFormat;


/**
 *
 * @author Saptaji
 */
public class Add_User extends javax.swing.JFrame {
    Connection con;
    PreparedStatement psmt;
    
    public Add_User() {
        setTitle("FORM TAMBAH USER");
        initComponents();
        tx_fname.setDocument(new onlyLetter().getOnlyLetter());
        tx_lname.setDocument(new onlyLetter().getOnlyLetter());
        l_jabatan.setVisible(false);
        combo_kategori.setVisible(false);
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lb_judul = new javax.swing.JLabel();
        lb_username = new javax.swing.JLabel();
        tx_fname = new javax.swing.JTextField();
        b_tambah = new javax.swing.JButton();
        lb_id = new javax.swing.JLabel();
        tx_nik = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        tx_lname = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        l_jabatan = new javax.swing.JLabel();
        tx_pass = new javax.swing.JPasswordField();
        tx_rpass = new javax.swing.JPasswordField();
        b_clear = new javax.swing.JButton();
        combo_kategori = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        lb_email = new javax.swing.JLabel();
        tx_email = new javax.swing.JTextField();
        b_view = new javax.swing.JButton();
        lb_gender = new javax.swing.JLabel();
        combo_gender = new javax.swing.JComboBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMnKembali = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        lb_judul.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        lb_judul.setText("Tambah User");

        lb_username.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lb_username.setText("First Name");

        tx_fname.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tx_fname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tx_fnameActionPerformed(evt);
            }
        });

        b_tambah.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        b_tambah.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/save.png"))); // NOI18N
        b_tambah.setText("SIMPAN DATA USER");
        b_tambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_tambahActionPerformed(evt);
            }
        });

        lb_id.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lb_id.setText("NIK");

        tx_nik.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Last Name");

        tx_lname.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Password");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Re-Password");

        l_jabatan.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        l_jabatan.setText("Jabatan");

        tx_pass.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        tx_rpass.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        b_clear.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        b_clear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/clear.png"))); // NOI18N
        b_clear.setText("BERSIHKAN");
        b_clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_clearActionPerformed(evt);
            }
        });

        combo_kategori.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        combo_kategori.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "User" }));
        combo_kategori.setEnabled(false);
        combo_kategori.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_kategoriActionPerformed(evt);
            }
        });

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/add_user2.png"))); // NOI18N

        lb_email.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lb_email.setText("Email");

        tx_email.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        b_view.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/view_user.png"))); // NOI18N
        b_view.setText("VIEW USER");
        b_view.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_viewActionPerformed(evt);
            }
        });

        lb_gender.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lb_gender.setText("Gender");

        combo_gender.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        combo_gender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Male", "Female" }));

        jMenuBar1.setBackground(new java.awt.Color(236, 236, 236));

        jMnKembali.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/back.png"))); // NOI18N
        jMnKembali.setToolTipText("Menu Utama");
        jMnKembali.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMnKembaliMousePressed(evt);
            }
        });
        jMenuBar1.add(jMnKembali);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(lb_judul))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(b_view))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(l_jabatan)
                            .addComponent(lb_id, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lb_username, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lb_gender))
                                .addGap(21, 21, 21)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tx_fname, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(combo_gender, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(combo_kategori, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tx_nik, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(lb_email)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1))
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tx_rpass, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tx_email, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tx_pass, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tx_lname, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(b_tambah)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(b_clear)))
                        .addGap(0, 47, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jLabel5))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(b_view)
                        .addGap(20, 20, 20)
                        .addComponent(lb_judul)))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lb_id)
                    .addComponent(tx_nik, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lb_gender)
                    .addComponent(combo_gender, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_jabatan)
                    .addComponent(combo_kategori, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tx_fname, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lb_username))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tx_lname, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tx_pass, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tx_rpass, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lb_email)
                    .addComponent(tx_email, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b_tambah)
                    .addComponent(b_clear, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void clear_text()
    {
        tx_nik.setText("");
        tx_fname.setText("");
        tx_lname.setText("");
        tx_pass.setText("");
        tx_rpass.setText("");
        tx_email.setText("");
    }
    private void b_tambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_tambahActionPerformed
       
        try{
           //tx_fname = new JFormattedTextField(NumberFormat.getInstance());
           if(tx_nik.getText().equals("")){JOptionPane.showMessageDialog(null,"NIK harus diisi", "ERROR", JOptionPane.PLAIN_MESSAGE);}
           else if(tx_fname.getText().equals("")){JOptionPane.showMessageDialog(null,"Nama Depan harus diisi", "ERROR", JOptionPane.PLAIN_MESSAGE);}
           else if(tx_lname.getText().equals("")){JOptionPane.showMessageDialog(null,"Nama Belakang harus diisi", "ERROR", JOptionPane.PLAIN_MESSAGE);}
           else if(tx_pass.getText().equals("")){JOptionPane.showMessageDialog(null,"Password Harus Diisi", "ERROR", JOptionPane.PLAIN_MESSAGE);}
           else if(tx_pass.getText().length()<5){JOptionPane.showMessageDialog(null,"Password tidak boleh kurang dari 5 digit", "ERROR", JOptionPane.PLAIN_MESSAGE);}
           else if(!tx_rpass.getText().equals(tx_pass.getText())){JOptionPane.showMessageDialog(null,"Re-Pass tidak sama", "ERROR", JOptionPane.PLAIN_MESSAGE);}
           else if(tx_email.getText().equals("")){JOptionPane.showMessageDialog(null,"Email Harus Diisi", "ERROR", JOptionPane.PLAIN_MESSAGE);}
           else{
                String nik=tx_nik.getText();
                con = Koneksi.getConnection();
                Statement st=con.createStatement();
                String sql2="Select nik FROM users Where nik='" + nik + "'";
                ResultSet rs=st.executeQuery(sql2);
                if(rs.next()){JOptionPane.showMessageDialog(null,"NIK "+nik+" Sudah ada Di DataBase", "ERROR", JOptionPane.PLAIN_MESSAGE);}
                else{
                
                    String sql1 = "Insert into users "+"values(?,?,?,?,?,?,?)";
                    String password = tx_pass.getText();
                    Generate_md5 get = new Generate_md5();
                    String key = get.md5(password);
                    psmt = con.prepareStatement(sql1);
                    psmt.setString(1,tx_nik.getText());
                    psmt.setString(2,(String)combo_gender.getSelectedItem());
                    psmt.setString(3,tx_fname.getText());
                    psmt.setString(4,tx_lname.getText());
                    psmt.setString(5,key);
                    psmt.setString(6,(String)combo_kategori.getSelectedItem());
                    psmt.setString(7,tx_email.getText());
                    psmt.executeUpdate();
                    JOptionPane.showMessageDialog(null, "data telah ditambah");
                    psmt.close();
                }
           }
           
       }
       catch(Exception e)
       {
           JOptionPane.showMessageDialog(null, e);
       }
           
    }//GEN-LAST:event_b_tambahActionPerformed

    private void b_clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_clearActionPerformed
        // TODO add your handling code here:
        clear_text();
    }//GEN-LAST:event_b_clearActionPerformed

    private void combo_kategoriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_kategoriActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_combo_kategoriActionPerformed

    private void jMnKembaliMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMnKembaliMousePressed
        // TODO add your handling code here:
        new AdminForm().setVisible(true);
        dispose();
    }//GEN-LAST:event_jMnKembaliMousePressed

    private void b_viewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_viewActionPerformed
        // TODO add your handling code here:
        new Tampil_User().setVisible(true);
        dispose();
    }//GEN-LAST:event_b_viewActionPerformed

    private void tx_fnameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tx_fnameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tx_fnameActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        new AdminForm().setVisible(true);
        dispose();
    }//GEN-LAST:event_formWindowClosing
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Add_User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Add_User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Add_User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Add_User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Add_User().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_clear;
    private javax.swing.JButton b_tambah;
    private javax.swing.JButton b_view;
    private javax.swing.JComboBox combo_gender;
    private javax.swing.JComboBox combo_kategori;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMnKembali;
    private javax.swing.JLabel l_jabatan;
    private javax.swing.JLabel lb_email;
    private javax.swing.JLabel lb_gender;
    private javax.swing.JLabel lb_id;
    private javax.swing.JLabel lb_judul;
    private javax.swing.JLabel lb_username;
    private javax.swing.JTextField tx_email;
    private javax.swing.JTextField tx_fname;
    private javax.swing.JTextField tx_lname;
    private javax.swing.JTextField tx_nik;
    private javax.swing.JPasswordField tx_pass;
    private javax.swing.JPasswordField tx_rpass;
    // End of variables declaration//GEN-END:variables
}
